package com.hdoit.ares.web.tp.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.tp.model.TestCarCollection;
import com.hdoit.ares.logical.tp.service.ITestCarCollectionService;
import com.hdoit.ares.logical.viewmodel.tp.TestCarCollectionSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年01月26日 09:12:33
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/zzb/testCarCollection/")
public class TestCarCollectionController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/zzb/testCarCollection/";
    }

    private ITestCarCollectionService testCarCollectionService;
    public ITestCarCollectionService getTestCarCollectionService() {
        return testCarCollectionService;
    }
    @Resource
    public void setTestCarCollectionService(ITestCarCollectionService testCarCollectionService) {
        this.testCarCollectionService = testCarCollectionService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="testCarCollectionDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
/*    private final String provinceCityViewName = "provinceCity";*/

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,TestCarCollectionSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(testCarCollectionService.queryCount(search));
        List<TestCarCollection> testCarCollections= testCarCollectionService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("testCarCollections", testCarCollections);
        model.addAttribute("testCarCollectionSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String testCarCollectionDetailed(Model model,String key){
        TestCarCollection testCarCollection=new TestCarCollection();
        if(!StringUtils.isEmpty(key)){
            testCarCollection=testCarCollectionService.queryModel(key);
        }
        model.addAttribute("testCarCollection", testCarCollection);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,TestCarCollection testCarCollection){
        if(StringUtils.isEmpty(testCarCollection.getId())){
            testCarCollection.setModelStatus(ModelStatus.add);
        }else{
            testCarCollection.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(testCarCollectionService.applyChange(testCarCollection)){
            actionResult=new ActionResult();
            
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("testCarCollection", testCarCollection);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(testCarCollectionService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
